package com.quick.controller.system;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.quick.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.system.domain.SysMenu;
import com.quick.system.service.SysMenuService;
import com.quick.common.utils.poi.ExcelUtil;
/**
 * 菜单权限Controller
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Api(value = "菜单权限管理", tags = {"菜单权限操作接口"})
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

   /**
    * 查询菜单权限列表
    */
    @PreAuthorize("@ps.hasPermission('system:menu:list')" )
    @GetMapping("/list")
    @ApiOperation(value = "查询菜单权限列表")
    public ApiResponse list(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.selectSysMenuList(sysMenu);
        return total(list);
    }

    /**
     * 导出菜单权限列表
     */
    @PreAuthorize("@ps.hasPermission('system:menu:export')" )
    @Log(title = "菜单权限", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出菜单权限列表")
    public void export(HttpServletResponse response, SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.selectSysMenuList(sysMenu);
        ExcelUtil<SysMenu> util = new ExcelUtil<>(SysMenu.class);
        util.exportExcel(response, list, "菜单权限数据");
    }

    /**
     * 获取菜单权限详细信息
     */
    @PreAuthorize("@ps.hasPermission('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    @ApiOperation(value = "获取菜单权限详细信息")
    public ApiResponse getInfo(@PathVariable("menuId") Long menuId) {
        return success(sysMenuService.selectSysMenuByMenuId(menuId));
    }

    /**
     * 新增菜单权限
     */
    @PreAuthorize("@ps.hasPermission('system:menu:add')")
    @Log(title = "菜单权限", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增菜单权限")
    public ApiResponse add(@RequestBody SysMenu sysMenu) {
        return success(sysMenuService.insertSysMenu(sysMenu));
    }

    /**
     * 修改菜单权限
     */
    @PreAuthorize("@ps.hasPermission('system:menu:edit')")
    @Log(title = "菜单权限", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改菜单权限")
    public ApiResponse edit(@RequestBody SysMenu sysMenu) {
        return success(sysMenuService.updateSysMenu(sysMenu));
    }

    /**
     * 删除菜单权限
     */
    @PreAuthorize("@ps.hasPermission('system:menu:remove')")
    @Log(title = "菜单权限", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{menuIds}")
    @ApiOperation(value = "删除菜单权限")
    public ApiResponse remove(@PathVariable Long[] menuIds) {
        return success(sysMenuService.deleteSysMenuByMenuIds(menuIds));
    }

    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ps.hasPermission('system:menu:list')" )
    @GetMapping("/treeRole/{roleId}")
    @ApiOperation(value = "查询菜单权限列表")
    public ApiResponse treeRole(@PathVariable Long roleId) {
        List<SysMenu> list = sysMenuService.selectSysMenuList(new SysMenu());
        Map<String, Object> data = new HashMap<>(2);
        data.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        data.put("menus", sysMenuService.buildMenusTreeSelect(list));
        return success(data);
    }
    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ps.hasPermission('system:menu:list')" )
    @GetMapping("/tree")
    @ApiOperation(value = "查询菜单权限列表")
    public ApiResponse tree() {
        List<SysMenu> list = sysMenuService.selectSysMenuList(new SysMenu());
        return success(sysMenuService.buildMenusTreeSelect(list));
    }
}
